package com.robot.mvvm.vm.main

import android.app.Application
import android.text.TextUtils
import androidx.databinding.ObservableField
import api.IotServiceFactory
import com.blankj.utilcode.util.StringUtils
import com.robot.base.base.vm.GLBaseViewModel
import com.robot.base.common.api.AbstractViewModelSubscriber
import com.robot.base.configs.AppConstants
import com.robot.base.model.QuestionNumBean
import com.robot.base.util.arouter.ARouterUtil
import com.robot.mvvm.R
import me.goldze.mvvmhabit.binding.command.BindingAction
import me.goldze.mvvmhabit.binding.command.BindingCommand
import me.goldze.mvvmhabit.http.BaseResponse

class WorkOrderViewModel(application: Application) : GLBaseViewModel(application) {

    val repairText = ObservableField<String>()
    val inspectionText = ObservableField<String>()


    val toRepairWorkOrderCommand = BindingCommand<Any>(BindingAction {
        ARouterUtil.goToActivity(AppConstants.Router.Main.A_REPAIR_WORK_ORDER)
    })
    fun initData() {
        repairText.set(
            "${StringUtils.getString(R.string.app_tips_52)}${0}${StringUtils.getString(R.string.app_tips_53)}"
        )
        inspectionText.set(
            "${StringUtils.getString(R.string.app_tips_52)}${0}${StringUtils.getString(R.string.app_tips_53)}"
        )
        getQuestionNum()
    }

    private fun getQuestionNum() {
        showLoadingDialog()
        IotServiceFactory.getQuestionNum().subscribe(
            object : AbstractViewModelSubscriber<BaseResponse<QuestionNumBean>>(this) {
                override fun onNext(t: BaseResponse<QuestionNumBean>) {
                    dismissLoadingDialog()
                    repairText.set(
                        "${StringUtils.getString(R.string.app_tips_52)}${
                            if (TextUtils.isEmpty(t.data.totalNum)) {
                                0
                            } else {
                                t.data.totalNum
                            }
                        }${StringUtils.getString(R.string.app_tips_53)}"
                    )
                }
            }
        )
    }

}